<!--
 * @Author: your name
 * @Date: 2022-03-29 16:42:12
 * @LastEditTime: 2022-03-29 17:04:44
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \geyao\geyao\原型链继承220329.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>原型链继承</title>
</head>

<body>
  <script>
    function father() {
      this.show = "我是你爸爸";
    }

    father.prototype.getSuperValue = function () {
      return this.show;
    }

    function son() {
      this.sonshow = "我是你儿子";
    }

    // 这里是关键，创建爸爸的实例，并将该实例赋值给儿子
    son.prototype = new father();

    son.prototype.getSubValue = function () {
      return this.sonshow;
    }

    var instance = new son();
    console.log(instance.getSuperValue()); // 我是你爸爸
    //继承的本质就是复制，即重写原型对象，代之以一个新类型的实例。
    //原型链方案存在的缺点：多个实例对引用类型的操作会被篡改。
  </script>
</body>

</html>